package com.qpl.depression.cms.controller;

import com.qpl.depression.cms.common.ServerResponse;
import com.qpl.depression.cms.entity.Video;
import com.qpl.depression.cms.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 宣传视频页面
     *
     * @return
     */
    @GetMapping("video")
    public String videoPage() {
        return "video";
    }

    /**
     * 宣传视频数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("video/data")
    public ServerResponse videData() {
        List<Video> videos = videoService.list();
        return ServerResponse.createBySuccess("success", videos);
    }

    /**
     * 宣传视频编辑
     *
     * @return
     */
    @GetMapping("edit/video")
    public String editVideo(Long id, ModelMap modelMap) {
        Video video = videoService.getById(id);
        modelMap.addAttribute("video", video);
        return "videoEditPage";
    }

    /**
     * 宣传视频保存
     *
     * @return
     */
    @PostMapping("/video/save")
    @ResponseBody
    public ServerResponse videoSave(Video video) {
        return videoService.updateById(video) ? ServerResponse.createBySuccessMessage("更新成功") : ServerResponse.createByErrorMessage("更新失败");
    }
}
